//
//  ViewController.swift
//  Example
//
//  Created by 山神 on 2021/9/3.
//

import UIKit
import ClipImageBrowser

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let bgImageView = UIImageView(image: UIImage(named: "1"))
        view.addSubview(bgImageView)

        let nextButton = UIButton(frame: CGRect(x: 300, y: 100, width: 66, height: 44))
        nextButton.setTitle("裁剪", for: .normal)
        nextButton.backgroundColor = .green
        view.addSubview(nextButton)
        nextButton.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
    }

    @objc func nextAction() {
        let controller = ClipImageBrowserController(image: UIImage(named: "1")!, direction: .up)
        controller.delegate = self
        present(controller, animated: true, completion: nil)
    }
}

extension ViewController: ClipImageBrowserProtocol {
    func clickSureButton(controller: ClipImageBrowserController, clipImage: UIImage, clipImageRect: CGRect) {
        let temp = UIImageView(image: clipImage)
        temp.frame = CGRect(x: 10, y: 50, width: clipImageRect.width, height: clipImageRect.width)
        view.addSubview(temp)
        DispatchQueue.main.asyncAfter(wallDeadline: .now() + 3) {
            temp.removeFromSuperview()
        }
    }
}
